package com.kly.cashmall.module.index;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.gongwen.marqueen.SimpleMF;
import com.gongwen.marqueen.SimpleMarqueeView;
import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarFragment;
import com.kly.cashmall.base.app_action.ActionHeads;
import com.kly.cashmall.base.app_action.BaseActionHelper;
import com.kly.cashmall.bean.CenterVOBean;
import com.kly.cashmall.bean.IndexBean;
import com.kly.cashmall.event.HomeEvent;
import com.kly.cashmall.framework.mvp.PresenterLifeCycle;
import com.kly.cashmall.module.index.adapter.IndexProductAdapter;
import com.kly.cashmall.module.index.presenter.IndexPresenter;
import com.kly.cashmall.module.index.presenter.IndexViewer;
import com.kly.cashmall.utils.data.CollectionUtils;
import com.kly.cashmall.utils.function.GlideUtils;
import com.kly.cashmall.utils.data.TextUtil;
import com.kly.cashmall.utils.ViewHolder;
import com.kly.cashmall.widget.mzbanner.MZBannerView;
import com.kly.cashmall.widget.mzbanner.holder.MZHolderCreator;
import com.kly.cashmall.widget.mzbanner.holder.MZViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Collections;
import java.util.List;

/**
 * @author
 */
public class IndexFragment extends BaseBarFragment implements IndexViewer {

    @PresenterLifeCycle
    IndexPresenter homePresenter = new IndexPresenter(this);
    private RecyclerView mPreferredListRecycler;
    private IndexProductAdapter mAdapter;
    private MZBannerView mTopShuffling;
    private SimpleMarqueeView<String> marqueeView;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_index_authorized;
    }

    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setActionBar(false);
        EventBus.getDefault().register(this);
        mTopShuffling = bindView(R.id.top_shuffling);
        mTopShuffling.setBannerItemMargin(20, 0, 20, 0);
        marqueeView = bindView(R.id.simple_marquee_view);
        marqueeView.setAnimateFirstView(true);
        mPreferredListRecycler = (RecyclerView) findViewById(R.id.preferred_list_recycler);
        mAdapter = new IndexProductAdapter(null);
        mPreferredListRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        mPreferredListRecycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (mAdapter.getItem(position).isPermitApply()) {
                    BaseActionHelper.with(getContext()).handleAction(mAdapter.getItem(position).getButtonJumpUrl(), true);
                }
            }
        });
        bindView(R.id.img_notice,v -> {
            BaseActionHelper.with(getActivity()).handleAction(ActionHeads.LMActionNotice,true);
        });
    }

    @Override
    protected void loadData() {
//        homePresenter.getProductHome();
    }

    @Override
    public void onGetProductHomeSuccess(IndexBean home) {
        bindView(R.id.bg_unified_toolbar, true);

        // 设置数据
        mTopShuffling.setPages(home.getRecommendationList(), new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        mTopShuffling.start();

        List<String> noticeList = home.getNoticeList();
        if (!CollectionUtils.isEmpty(noticeList)){
            bindView(R.id.marquee_view_container,true);
            SimpleMF<String> marqueeFactory = new SimpleMF(getActivity());
            Collections.shuffle(noticeList);
            marqueeFactory.setData(noticeList);
            marqueeView.setMarqueeFactory(marqueeFactory);
            marqueeView.startFlipping();
        }


        //产品列表
        if (!CollectionUtils.isEmpty(home.getMoreRecommendationList())) {
            bindView(R.id.cl_product_module, true);
            mAdapter.setList(home.getMoreRecommendationList());
        }
    }

    public static class BannerViewHolder implements MZViewHolder<CenterVOBean> {
        private ViewHolder holder;

        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.layout_product_detail_home, null);
            holder = ViewHolder.getHolder(view);
            return view;
        }

        @Override
        public void onBind(Context context, int i, CenterVOBean centerVO) {
            // 数据绑定
            holder.setText(R.id.recommended_title, centerVO.getCategoryName());
            SpannableString string = new SpannableString(centerVO.getCategoryRange());
            if (centerVO.getCategoryRange().contains("₹")) {
                string.setSpan(new RelativeSizeSpan(0.8f), centerVO.getCategoryRange().indexOf("₹"), centerVO.getCategoryRange().indexOf("₹") + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            holder.setText(R.id.recommended_amount, string);
            holder.setEnable(R.id.recommended_apply,centerVO.isPermitApply());
            holder.setText(R.id.recommended_apply, centerVO.getButtonName());
            holder.setClickListener(R.id.recommended_apply, v -> {
                if (centerVO.isPermitApply()) {
                    BaseActionHelper.with(context).handleAction(centerVO.getButtonJumpUrl(),true);
                }
            });
        }


    }

    @Override
    public void onResume() {
        super.onResume();
        homePresenter.getProductHome();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onHomeEvent(HomeEvent homeEvent) {
        homePresenter.getProductHome();
    }


    @Override
    protected void willDestroy() {
        super.willDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            homePresenter.getProductHome();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        marqueeView.startFlipping();
    }

    @Override
    public void onStop() {
        super.onStop();
        marqueeView.stopFlipping();
    }
}
